home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / dim.cpp < prev    next >
C/C++ Source or Header  |  1994-10-10  |  4KB  |  144 lines

  1. #include "dim.h"
  2. #include "event.h"
  3. #include "global.h"
  4. #include "pcx.h"
  5.  
  6.  
  7. #define MDN (MCleftDn|MCrightDn)
  8.  
  9. #ifndef DATA_TOOLS
  10. #define DATA_TOOLS 1
  11. #endif  DATA_TOOLS
  12.  
  13.  
  14. void show_fig(int fig_type, rect start)
  15.     {
  16.     switch(fig_type)
  17.     {
  18.     case FIG_RECTANGLE:
  19.         ::rectangle(start);
  20.         break;
  21.     case FIG_LINE:
  22.         ::line(start.origin, start.corner);
  23.         break;
  24.     case FIG_CIRCLE:
  25.         setlinestyle(SOLID_LINE, 1, 3);
  26.         ::ellipse(start.origin.X + start.width() / 2,
  27.             start.origin.Y + start.height() / 2, 0, 360,
  28.             start.width() / 2, start.height() / 2);
  29.         break;
  30.         case FIG_PCX:
  31.             pcx_file_scr("copy.pcy", start.origin, NULL, XOR_PUT);
  32.             break;
  33.     }
  34.  
  35.     }
  36. //////////////////////
  37. void hide_fig(int fig_type, rect start)
  38.     {
  39.     show_fig(fig_type, start);
  40.     }
  41. //////////////////////
  42. rect change_pos(int fig_type, int act_type, rect start, loc minsize,
  43.                 loc step, rect r)
  44.     {
  45.     loc delta(0, 0);
  46.     if(e.what == MOUSEEVENT)   // it is mouse
  47.     delta = e.where() - start.corner;
  48.     else                       // it is key
  49.     switch(e.key)
  50.         {
  51.         case EVENT_LEFT:
  52.         if(fig_type == FIG_LINE || (start.width() >= minsize.X
  53.             && start.corner.X > minsize.X))
  54.             delta = loc(-step.X, 0);
  55.         break;
  56.         case EVENT_RIGHT:
  57.         if(start.corner.X <= getmaxx() - step.X)
  58.             delta = loc(step.X, 0);
  59.         break;
  60.         case EVENT_UP:
  61.         if(fig_type == FIG_LINE || (start.height() >= minsize.Y
  62.             && start.corner.Y > minsize.Y))
  63.             delta = loc(0, -step.Y);
  64.         break;
  65.         case EVENT_DN:
  66.         if(start.corner.Y <= getmaxx() - step.Y)
  67.             delta = loc(0, step.Y);
  68.         break;
  69.         default: return start;
  70.         }
  71.  
  72.     rect ok;
  73.  
  74.     if(act_type == MOVE)
  75.     ok = start + delta;
  76.     else                               // RESIZE
  77.     {
  78.     if((fig_type != FIG_LINE)
  79.         && (start.corner.X + delta.X < start.origin.X + minsize.X
  80.         || start.corner.Y + delta.Y < start.origin.Y + minsize.Y))
  81.         delta = loc(0, 0);
  82.         ok = rect(start.origin, start.corner + delta);
  83.     }
  84.  
  85. //    ok.sort();
  86.  
  87.     mouseSetPosition(ok.corner);
  88.     if(fig_type == FIG_CIRCLE && (ok.width() < 0 || ok.height() < 0))
  89.     {
  90.     ok = rect(ok.origin, ok.origin);
  91.     }
  92.     if(!r.contains(ok))
  93.         ok = start;
  94.     hide_fig(fig_type, start);
  95.     show_fig(fig_type, ok);
  96.  
  97.     return ok;
  98.     }
  99. /////////////////////////////////
  100. rect get_dim(int fig_type, int act_type, rect r, rect start, loc minsize,
  101.     loc step)
  102.     {
  103.     mouseSetPosition(start.corner);
  104.     readevent(MOUSEEVENT);
  105.     mouseSetRange(rect(r.origin + minsize, r.corner));
  106.     mouseHideCursor();
  107.     setwritemode(XOR_PUT);
  108.     int color = getcolor();
  109.     setcolor(WHITE);
  110.  
  111.     setlinestyle(SOLID_LINE, 1, 1);
  112.  
  113.     show_fig(fig_type, start);                 // show ruber figure first time
  114.  
  115.     int key;
  116.     while(1)
  117.     {
  118.     if(scriptMode == GO)
  119.         e = getevent(KEYEVENT | MOUSEEVENT,
  120.              MCmove | MCleftDn | MCrightDn);
  121.     else
  122.         get_event();
  123.  
  124.     if((e.what == MOUSEEVENT && (key = e.mousechange & MDN))   // mouse button pressed
  125.         || (e.what == KEYEVENT
  126.         && (e.key == EVENT_RETURN || e.key == EVENT_ESC)))
  127.         {
  128.         hide_fig(fig_type, start);        // hide ruber figure last time
  129.         mouseShowCursor();
  130.         mouseDefaultRange();
  131.  
  132.         setwritemode(COPY_PUT);
  133.         setcolor(color);
  134.  
  135.         return (e.what == KEYEVENT && e.key == EVENT_RETURN)
  136.         || (e.what == MOUSEEVENT && key == MCleftDn)  // left - do it,
  137.         ? start : rect(0, 0, 0, 0);             // right - cancel
  138.         }
  139.  
  140.     start = change_pos(fig_type, act_type, start, minsize, step, r);
  141.  
  142.     }
  143.  
  144.     }